home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / jpegagasrc / jpegaga / display.c < prev    next >
C/C++ Source or Header  |  1995-11-08  |  13KB  |  555 lines

  1. /* Screen display routines              */
  2. /* written by Günther Röhrich           */
  3. /* this source is Public Domain         */
  4.  
  5. /* this works only with OS 3.0 or higher */
  6.  
  7. /* last change: 1-Feb-1995 */
  8.  
  9. #pragma regcall(newWPL8(a0,d0,d1,d2,a2))
  10.  
  11. #define INTUI_V36_NAMES_ONLY
  12.  
  13. #include <exec/types.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/screens.h>
  16. #include <graphics/modeid.h>
  17. #include "WBFlags.h"
  18.  
  19. #define __NOLIBBASE__
  20. #include <proto/intuition.h>
  21. #include <proto/exec.h>
  22. #include <proto/graphics.h>
  23.  
  24. #ifdef __GNUC__
  25. extern __inline void
  26. newWPL8(struct RastPort *rp, unsigned long xstart, unsigned long ystart,
  27.         unsigned long width, UBYTE *array)
  28. {
  29.   register struct RastPort *a0 __asm("a0") = rp;
  30.   register unsigned long d0 __asm("d0") = xstart;
  31.   register unsigned long d1 __asm("d1") = ystart;
  32.   register unsigned long d2 __asm("d2") = width;
  33.   register UBYTE *a2 __asm("a2") = array;
  34.   __asm __volatile ("jsr _newWPL8"
  35.   : /* no output */
  36.   : "r" (a0), "r" (d0), "r" (d1), "r" (d2), "r" (a2)
  37.   : "a0","a1","a2","d0","d1","d2", "memory");
  38. }
  39. #endif
  40.  
  41.  
  42. #define HAM8 1
  43. #define ERROR_INTUITION 2
  44. #define ERROR_GRAPHICS 3
  45.  
  46. struct IntuitionBase *IntuitionBase = NULL;
  47. struct GfxBase *GfxBase = NULL;
  48. extern struct Library *IconBase;
  49.  
  50. extern struct WBFlags WBFlags;
  51. extern short DoNotWait;
  52. extern short finish;
  53.  
  54. extern ULONG SMR_DisplayID;
  55. extern ULONG *ScreenColorTable;
  56. extern ULONG ScaleFitDisplayID;
  57.  
  58. /* for newWPL8() */
  59. PLANEPTR PlanePos[8];
  60. UBYTE    *linebuf = NULL;
  61.  
  62. struct BitMap *my_tempbitmap = NULL;
  63. struct BitMap *my_bitmap[2] = {NULL,NULL};
  64. struct Screen *my_tempscreen  = NULL;
  65. struct Screen *my_screen[2] = {NULL, NULL};
  66. struct Window *my_tempwindow =  NULL;
  67. struct Window *my_window[2] = {NULL, NULL};
  68. int Drow; 
  69.  
  70. struct TagItem MyScreenTags[] = {
  71.  
  72. { SA_Width,     (ULONG)0 },
  73. { SA_Height,    (ULONG)0 },
  74. { SA_Depth,     (ULONG)0 },
  75. { SA_DisplayID, (ULONG)0 },
  76. { SA_Behind,    (ULONG)FALSE },
  77. { SA_Colors32,  NULL},
  78. { SA_Parent,    NULL },
  79. { SA_BitMap,    NULL},
  80. { SA_Type,      (ULONG)CUSTOMSCREEN|CUSTOMBITMAP },
  81. { SA_Quiet,     (ULONG)TRUE },
  82. { SA_AutoScroll,(ULONG)TRUE },
  83. { SA_Overscan,  (ULONG)OSCAN_STANDARD },
  84. { TAG_DONE,     (ULONG)TRUE }  };
  85.  
  86. struct TagItem MyWindowTags[] = {
  87.  
  88. { WA_Width,        (ULONG)0 },
  89. { WA_Height,       (ULONG)0 },
  90. { WA_CustomScreen, NULL },
  91. { WA_Activate,     (ULONG)TRUE },
  92. { WA_Left,         (ULONG)0 },
  93. { WA_Top,          (ULONG)0 },
  94. { WA_NoCareRefresh,(ULONG)TRUE },
  95. { WA_Borderless,   (ULONG)TRUE },
  96. { WA_Backdrop,     (ULONG)TRUE },
  97. { WA_RMBTrap,      (ULONG)TRUE }, /* disable screen menu drawing */
  98. { WA_IDCMP,        (ULONG)IDCMP_MOUSEBUTTONS | IDCMP_VANILLAKEY },
  99. { WA_BusyPointer,  (ULONG)TRUE }, /* V39 only! */
  100. { TAG_DONE,        (ULONG)TRUE } };
  101.  
  102. void CloseDisplay(void);
  103. void FlipScreen(void);
  104. void FinalWait(void);
  105.  
  106. /* Initialize the display, colors will be set later */
  107. /* if there is a screen open it in the background as my_screen[1] */
  108.  
  109.  
  110. int InitDisplay(int cols, int rows, ULONG Mode, int NumPlanes)
  111. {
  112.   ULONG Depth, DisplayID;
  113.   ULONG NoSUPER72, i=0;
  114.  
  115.   Drow=0;
  116.  
  117.  
  118.   if(!IntuitionBase)
  119.   {
  120.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39)))
  121.     {
  122.      printf("Could not open intuition.library V39 or higher.\n"); 
  123.      return 0;
  124.     }
  125.   }
  126.  
  127.   if(!GfxBase)
  128.   {
  129.     if(!(GfxBase = (struct GfxBase *)OpenLibrary((UBYTE *)"graphics.library",39)))
  130.     {
  131.       printf("Could not open graphics.library V39 or higher.\n");
  132.       return 0;
  133.     }
  134.   }
  135.  
  136.   /* If we already have two screens we must close one */
  137.  
  138.   if(my_screen[0] && my_screen[1])
  139.   {
  140.     FinalWait();
  141.     if(finish) return 0;
  142.     FlipScreen();
  143.   }
  144.  
  145.   if(WBFlags.SMRenable)
  146.   {
  147.     DisplayID = SMR_DisplayID;
  148.   }
  149.   else
  150.   {
  151.     /* Calculate a DisplayID */
  152.     /* this should be done better... */
  153.  
  154.     /* check if SUPER72 mode is available */
  155.  
  156.     NoSUPER72 = ModeNotAvailable(SUPER72_MONITOR_ID | SUPERHAMLACE_KEY);
  157.  
  158.     if(WBFlags.VGAenable)
  159.     {
  160.       if(Mode == HAM8)
  161.       {
  162.         if(cols > 780 && !NoSUPER72 && WBFlags.SUPER72enable) DisplayID = SUPER72_MONITOR_ID | SUPERHAMLACE_KEY;
  163.         else
  164.         if(cols > 370 || rows > 260)
  165.           DisplayID = VGAPRODUCTHAM_KEY;
  166.         else
  167.           DisplayID = VGALORESHAMDBL_KEY;
  168.       }
  169.       else
  170.       {
  171.         if(cols > 780 && !NoSUPER72 && WBFlags.SUPER72enable) DisplayID = SUPER72_MONITOR_ID | SUPERLACE_KEY;
  172.         else
  173.         if(cols > 370 || rows >260) 
  174.           DisplayID = VGAPRODUCT_KEY;
  175.         else
  176.           DisplayID = VGALORESDBL_KEY;        
  177.       }
  178.     }
  179.     else
  180.     {  
  181.       if(Mode == HAM8)
  182.       {
  183.         if(cols > 370 || rows > 260)
  184.           DisplayID = HIRESHAMLACE_KEY;
  185.         else
  186.           DisplayID = HAM_KEY;
  187.       }
  188.       else
  189.       {
  190.         if(cols > 370 || rows >260) 
  191.           DisplayID = HIRESLACE_KEY;
  192.         else
  193.           DisplayID = LORES_KEY; 
  194.       }   
  195.     }
  196.  
  197.     if(ScaleFitDisplayID)
  198.     {
  199.       DisplayID = ScaleFitDisplayID;
  200.       /* printf("ScaleFitDisplayID used.\n"); */
  201.     }
  202.   }
  203.  
  204.   if(Mode == HAM8) Depth = 8;
  205.     else Depth = NumPlanes;
  206.  
  207.  MyScreenTags[0].ti_Data = (ULONG)cols;
  208.  MyScreenTags[1].ti_Data = (ULONG)rows;
  209.  MyScreenTags[2].ti_Data = (ULONG)Depth;
  210.  MyScreenTags[3].ti_Data = (ULONG)DisplayID;
  211.  MyScreenTags[5].ti_Data = (ULONG)((void *)ScreenColorTable);
  212.  
  213.  MyWindowTags[0].ti_Data = (ULONG)cols;
  214.  MyWindowTags[1].ti_Data = (ULONG)rows;
  215.  
  216.   while(1)
  217.   {
  218.     if(my_tempbitmap == NULL)
  219.     {
  220.       my_tempbitmap = AllocBitMap(((cols+31)>>5)<<5,rows,Depth,BMF_CLEAR|BMF_DISPLAYABLE,NULL);
  221.       MyScreenTags[7].ti_Data = (ULONG)my_tempbitmap;
  222.     }
  223.  
  224.     if(my_tempbitmap == NULL)
  225.     {
  226.       /* do we already have a screen ? */
  227.       if(my_screen[0])
  228.       {
  229.         printf("Could not open background screen. (out of memory)\n");
  230.         if(!DoNotWait) FinalWait();
  231.         FlipScreen();
  232.         if(finish) return 0;
  233.         continue;
  234.       }
  235.       else return 0;
  236.     }
  237.  
  238.  
  239.     if(my_tempscreen == NULL && my_tempbitmap)
  240.     {
  241.       if(my_screen[0] == NULL)
  242.       {
  243.         MyScreenTags[4].ti_Data = (ULONG)FALSE;
  244.         MyScreenTags[6].ti_Tag = TAG_IGNORE;
  245.         my_tempscreen = OpenScreenTagList(NULL, (struct TagItem *)&MyScreenTags);
  246.       }
  247.       else
  248.       {
  249.         MyScreenTags[4].ti_Data = (ULONG)TRUE;
  250.         MyScreenTags[6].ti_Tag = SA_Parent;
  251.         MyScreenTags[6].ti_Data = (ULONG)((void *)my_screen[0]);
  252.         my_tempscreen = OpenScreenTagList(NULL, (struct TagItem *)&MyScreenTags);
  253.       }
  254.     }
  255.  
  256.     if(my_tempscreen == NULL)
  257.     {
  258.       /* do we already have a screen ? */
  259.       if(my_screen[0])
  260.       {
  261.         printf("Could not open background screen. (out of memory)\n");
  262.         if(!DoNotWait) FinalWait();
  263.         FlipScreen();
  264.         if(finish) return 0;
  265.         continue;
  266.       }
  267.       else return 0;
  268.     }
  269.     
  270.     /* open a dummy window to allow autoscroll feature      */
  271.   
  272.     if(my_tempwindow == NULL && my_tempscreen && my_tempbitmap)
  273.     {
  274.       MyWindowTags[2].ti_Data = (ULONG)((void *)my_tempscreen);
  275.       if(my_screen[0] == NULL)
  276.       {
  277.         MyWindowTags[3].ti_Data = (ULONG)TRUE;
  278.         my_tempwindow = OpenWindowTagList(NULL, (struct TagItem *)&MyWindowTags);
  279.       }
  280.       else
  281.       {
  282.         MyWindowTags[3].ti_Data = (ULONG)FALSE;
  283.         my_tempwindow = OpenWindowTagList(NULL, (struct TagItem *)&MyWindowTags);
  284.       }
  285.     }
  286.  
  287.     if(my_tempwindow == NULL)
  288.     {
  289.       if(my_tempscreen)
  290.       {
  291.         CloseScreen(my_tempscreen);
  292.         my_tempscreen = NULL;
  293.       }
  294.  
  295.       if(my_tempbitmap)
  296.       {
  297.         FreeBitMap(my_tempbitmap);
  298.         my_tempbitmap = NULL;
  299.       }
  300.  
  301.       /* do we already have a screen ? */
  302.       if(my_screen[0])
  303.       {
  304.         printf("Could not open window on background screen. (out of memory)\n");
  305.         /* wait for a mouse button press and close it */
  306.         if(!DoNotWait) FinalWait();
  307.         FlipScreen();
  308.         if(finish) return 0;
  309.         continue;
  310.       }
  311.       else return 0;
  312.     }
  313.  
  314.     /* Allocate line buffer for newWPL8 */
  315.  
  316.     if(linebuf) FreeVec(linebuf);
  317.     linebuf = AllocVec((ULONG)cols+32L, 0);
  318.     if(linebuf == NULL)
  319.     {
  320.       if(my_tempwindow)
  321.       {
  322.         CloseWindow(my_tempwindow);
  323.         my_tempwindow = NULL;
  324.       }
  325.       if(my_tempscreen)
  326.       {
  327.         CloseScreen(my_tempscreen);
  328.         my_tempscreen = NULL;
  329.       }
  330.  
  331.       if(my_tempbitmap)
  332.       {
  333.         FreeBitMap(my_tempbitmap);
  334.         my_tempbitmap = NULL;
  335.       }
  336.       
  337.       if(my_screen[0])
  338.       {
  339.         printf("Could not allocate line buffer (out of memory)\n");
  340.         if(!DoNotWait) FinalWait();
  341.         FlipScreen();
  342.         if(finish) return 0;
  343.         continue;
  344.       }
  345.       else return 0;
  346.     }
  347.     break;
  348.   }
  349.  
  350.   if(my_screen[0]) i=1;
  351.   my_bitmap[i] = my_tempbitmap;
  352.   my_screen[i] = my_tempscreen;
  353.   my_window[i] = my_tempwindow;
  354.   my_tempbitmap = NULL;
  355.   my_tempscreen = NULL;
  356.   my_tempwindow = NULL;  
  357.  
  358.   if(DoNotWait && my_screen[1]) FlipScreen();
  359.  
  360.   return 1; /* success */  
  361. }
  362.  
  363.  
  364.  
  365. /* Close the display */
  366.  
  367. void CloseDisplay(void)
  368. {
  369.   int i=0;
  370.   if(my_screen[1] && !finish)
  371.   {
  372.     FlipScreen();
  373.     FinalWait();
  374.   }
  375.   
  376.   if(my_screen[1]) i=1;
  377.  
  378.   if(linebuf)
  379.   {
  380.     FreeVec(linebuf);
  381.     linebuf = NULL;
  382.   }
  383.  
  384.   for(i=0; i < 2; i++)
  385.   {
  386.     if(my_screen[i]) ScreenToBack(my_screen[i]);
  387.     if(my_window[i])
  388.     {
  389.       CloseWindow(my_window[i]);
  390.       my_window[i] = NULL;
  391.     }
  392.     if(my_screen[i])
  393.     {
  394.       CloseScreen(my_screen[i]);
  395.       my_screen[i] = NULL;
  396.     }
  397.     if(my_bitmap[i])
  398.     {
  399.       FreeBitMap(my_bitmap[i]);
  400.       my_bitmap[i] = NULL;
  401.     }
  402.   }
  403.  
  404.   if(IntuitionBase)
  405.   {
  406.    CloseLibrary((struct Library *)IntuitionBase);
  407.    IntuitionBase = NULL;
  408.   }
  409.   if(GfxBase)
  410.   {
  411.     CloseLibrary((struct Library *)GfxBase);
  412.     GfxBase = NULL;
  413.   }
  414.   if(IconBase)
  415.   {
  416.     CloseLibrary(IconBase);
  417.     IconBase=NULL;
  418.   }
  419. }
  420.  
  421.  
  422. /* display a line of chunky pixel graphics... */
  423.  
  424.  
  425. void DisplayRow(char *array, int cols)
  426. {
  427.   int i=0;
  428.   if(my_screen[1]) i=1;
  429.   if(my_screen[i])
  430.   {
  431.    int j;
  432.    struct RastPort *MyRastPtr;
  433.    MyRastPtr = &my_screen[i]->RastPort;
  434.    for(j=0; j<8; j++)
  435.      PlanePos[j]=MyRastPtr->BitMap->Planes[j] + MyRastPtr->BitMap->BytesPerRow*Drow;
  436.    newWPL8(MyRastPtr, 0, Drow, cols, (UBYTE *)array);
  437.    Drow++;
  438.    /*  WritePixelLine8(&my_screen[i]->RastPort, 0, Drow++, cols, (UBYTE *)array, &temprp); */
  439.   }
  440.   
  441.    
  442. }
  443.  
  444.  
  445. /* check for a right mouse button press (or Esc press) */
  446.  
  447. int CheckButton(void)
  448. {
  449.   struct IntuiMessage *msg;
  450.   int Button = 0;
  451.  
  452.   if(my_window[0])
  453.   {
  454.     while(msg = (struct IntuiMessage *)GetMsg(my_window[0]->UserPort))
  455.     {
  456.       if(msg->Class == IDCMP_MOUSEBUTTONS)
  457.         if(msg->Code == MENUDOWN)
  458.           Button = 1; 
  459.       if(msg->Class == IDCMP_VANILLAKEY)
  460.         if(msg->Code == 27)  /* Esc */
  461.         {
  462.           Button = 2;
  463.           finish = 1;
  464.         }
  465.       ReplyMsg((struct Message *)msg);
  466.       if(Button) break;
  467.     }
  468.   }
  469.   if(Button && my_screen[1] && !finish)
  470.   {
  471.     FlipScreen();
  472.     Button = 0;
  473.   }
  474.   return Button;
  475. }
  476.        
  477. /* final wait after the picture is finished */
  478.  
  479. void FinalWait(void)
  480. {
  481.   struct IntuiMessage *msg;
  482.   int Button = 0;
  483.   struct TagItem WinPointerSet[] = { {WA_Pointer, NULL}, {TAG_DONE, NULL} };
  484.  
  485.   /* always wait for my_screen[0] */
  486.  
  487.  
  488.   /* set the normal pointer */
  489.   /* V39 function */
  490.  
  491.   if(!my_window[0]) return; 
  492.  
  493.   /* SetWindowPointer(my_window[0], WA_Pointer, NULL, TAG_DONE); */
  494.   SetWindowPointerA(my_window[0], WinPointerSet);
  495.   
  496.   if(my_window[0])
  497.   {
  498.     while(!Button)
  499.     {
  500.       Wait(1L<<my_window[0]->UserPort->mp_SigBit);
  501.       while(msg = (struct IntuiMessage *)GetMsg(my_window[0]->UserPort))
  502.       {
  503.         if(msg->Class == IDCMP_MOUSEBUTTONS)
  504.           if(msg->Code == MENUDOWN) Button = 1;
  505.         if(msg->Class == IDCMP_VANILLAKEY)
  506.           if(msg->Code == 27) /* Esc */
  507.           {
  508.             Button = 2;
  509.             finish = 1;
  510.           }
  511.         ReplyMsg((struct Message *)msg);
  512.         if(Button) break;
  513.       }
  514.     }
  515.   }
  516. }
  517.  
  518. void FlipScreen(void)
  519. {
  520.  if(my_screen[0] && my_screen[1])
  521.  {
  522.   ScreenDepth(my_screen[1], SDEPTH_TOFRONT | SDEPTH_INFAMILY, NULL);
  523.   if(my_window[1]) ActivateWindow(my_window[1]);
  524.  }
  525.  else if(my_screen[0]) ScreenToBack(my_screen[0]);
  526.  
  527.  if(my_window[0])
  528.  {
  529.    CloseWindow(my_window[0]);
  530.    my_window[0] = NULL;
  531.  }
  532.  
  533.  if(my_screen[0])
  534.  { 
  535.    CloseScreen(my_screen[0]);
  536.    my_screen[0] = NULL;
  537.  }
  538.  
  539.  if(my_bitmap[0])
  540.  {
  541.    FreeBitMap(my_bitmap[0]);
  542.    my_bitmap[0] = NULL;
  543.  }
  544.  
  545.  if(my_screen[1])
  546.  {
  547.    my_bitmap[0] = my_bitmap[1];
  548.    my_bitmap[1] = NULL;
  549.    my_screen[0] = my_screen[1];
  550.    my_screen[1] = NULL;
  551.    my_window[0] = my_window[1];
  552.    my_window[1] = NULL;
  553.  }
  554. }
  555.